/*
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
 * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
 * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

package com.swak.monitor.dubbo.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.swak.annotation.Delete;
import com.swak.annotation.Get;
import com.swak.annotation.Post;
import com.swak.annotation.Put;
import com.swak.annotation.RestApi;
import com.swak.monitor.dubbo.exception.ParamValidationException;
import com.swak.monitor.dubbo.exception.ResourceNotFoundException;
import com.swak.monitor.dubbo.model.dto.ConditionRouteDTO;
import com.swak.monitor.dubbo.model.dto.ConditionRouteResultDTO;
import com.swak.monitor.dubbo.service.RouteService;
import com.swak.monitor.dubbo.utils.Constants;
import com.swak.monitor.dubbo.utils.ConvertUtil;
import com.swak.utils.StringUtils;

@RestApi(value = "DubboConditionRoutesApi", path = "/api/monitor/:env/rules/route/condition")
public class ConditionRoutesApi {

	private final RouteService routeService;
	// private final ConsumerService consumerService;

	@Autowired
	public ConditionRoutesApi(RouteService routeService) {
		this.routeService = routeService;
	}

	@Post
	public boolean createRule(ConditionRouteDTO routeDTO, String env, String serviceVersion, String serviceGroup) {
		String serviceName = routeDTO.getService();
		String app = routeDTO.getApplication();
		if (StringUtils.isEmpty(serviceName) && StringUtils.isEmpty(app)) {
			throw new ParamValidationException("serviceName and app is Empty!");
		}
//        if (StringUtils.isNotEmpty(app) && consumerService.findVersionInApplication(app).equals("2.6")) {
//            throw new VersionValidationException("dubbo 2.6 does not support application scope routing rule");
//        }
		routeService.createConditionRoute(routeDTO, serviceVersion, serviceGroup);
		return true;
	}

	@Put("/:id")
	public boolean updateRule(String id, ConditionRouteDTO newConditionRoute, String env, String serviceVersion,
			String serviceGroup) {
		id = id.replace(Constants.ANY_VALUE, Constants.PATH_SEPARATOR);
		ConditionRouteDTO crDTO = new ConditionRouteDTO();
		String serviceName = newConditionRoute.getService();
		if (StringUtils.isNotEmpty(serviceName)) {
			id = ConvertUtil.getIdFromDTO(newConditionRoute, serviceVersion, serviceGroup);
		}
		crDTO.setService(id);
		ConditionRouteDTO oldConditionRoute = routeService.findConditionRoute(crDTO);
		if (oldConditionRoute == null) {
			throw new ResourceNotFoundException("can not find route rule for: " + id);
		}
		routeService.updateConditionRoute(newConditionRoute, serviceVersion, serviceGroup);
		return true;
	}

	@Get
	public List<ConditionRouteResultDTO> searchRoutes(String application, String service, String env,
			String serviceVersion, String serviceGroup) {
		ConditionRouteResultDTO conditionRoute;
		List<ConditionRouteResultDTO> result = new ArrayList<>();
		ConditionRouteDTO crDTO = new ConditionRouteDTO();
		if (StringUtils.isNotBlank(application)) {
			crDTO.setService(application);
			conditionRoute = routeService.findConditionRoute(crDTO);
		} else if (StringUtils.isNotBlank(service)) {
			crDTO.setService(service);
			String id = ConvertUtil.getIdFromDTO(crDTO, serviceVersion, serviceGroup);
			conditionRoute = routeService.findConditionRoute(id);
		} else {
			throw new ParamValidationException("Either Service or application is required.");
		}
		if (conditionRoute != null && conditionRoute.getConditions() != null) {
			result.add(conditionRoute);
		}
		return result;
	}

	@Get("/:id")
	public ConditionRouteResultDTO detailRoute(String id, String env, String serviceVersion, String serviceGroup,
			String scope) {
		id = id.replace(Constants.ANY_VALUE, Constants.PATH_SEPARATOR);
		ConditionRouteDTO crDTO = new ConditionRouteDTO();
		crDTO.setService(id);
		if (Constants.SERVICE.equals(scope)) {
			id = ConvertUtil.getIdFromDTO(crDTO, serviceVersion, serviceGroup);
			crDTO.setService(id);
		}
		ConditionRouteResultDTO conditionRoute = routeService.findConditionRoute(crDTO);
		if (conditionRoute == null || conditionRoute.getConditions() == null) {
			throw new ResourceNotFoundException("Unknown ID!");
		}
		return conditionRoute;
	}

	@Delete("/:id")
	public boolean deleteRoute(String id, String env, String serviceVersion, String serviceGroup, String scope) {
		id = id.replace(Constants.ANY_VALUE, Constants.PATH_SEPARATOR);
		ConditionRouteDTO crDTO = new ConditionRouteDTO();
		crDTO.setService(id);
		if (Constants.SERVICE.equals(scope)) {
			id = ConvertUtil.getIdFromDTO(crDTO, serviceVersion, serviceGroup);
			crDTO.setService(id);
		}
		routeService.deleteConditionRoute(crDTO);
		return true;
	}

	@Put("/enable/:id")
	public boolean enableRoute(String id, String env, String serviceVersion, String serviceGroup, String scope) {
		id = id.replace(Constants.ANY_VALUE, Constants.PATH_SEPARATOR);
		ConditionRouteDTO crDTO = new ConditionRouteDTO();
		crDTO.setService(id);
		if (Constants.SERVICE.equals(scope)) {
			id = ConvertUtil.getIdFromDTO(crDTO, serviceVersion, serviceGroup);
			crDTO.setService(id);
		}
		routeService.enableConditionRoute(crDTO);
		return true;
	}

	@Put("/disable/:id")
	public boolean disableRoute(String id, String env, String serviceVersion, String serviceGroup, String scope) {
		id = id.replace(Constants.ANY_VALUE, Constants.PATH_SEPARATOR);
		ConditionRouteDTO crDTO = new ConditionRouteDTO();
		crDTO.setService(id);
		if (Constants.SERVICE.equals(scope)) {
			id = ConvertUtil.getIdFromDTO(crDTO, serviceVersion, serviceGroup);
			crDTO.setService(id);
		}
		routeService.disableConditionRoute(crDTO);
		return true;
	}

}
